<?php

class Country_Model_DbTable_Managers extends Engine_Db_Table
{
  protected $_rowClass = 'Country_Model_Manager';
  
  public function getUserManagerId($country){
     $select = $this->select()
                    ->where('country_id = ?',$country);
     $rows = $this->fetchAll($select);
     
     $userIds = array();
     $userIds[0] = 0;
     foreach( $rows as $row ){
        $userIds[] = $row->user_id;   
     }
     return $userIds;
  }
  
  public function allowAddSystemField($user_id = 0){
    $viewer = Engine_Api::_()->user()->getViewer();
    if ($viewer->level_id == 1) return true;    
    if (empty($user_id)) {
        $user_id = $viewer->user_id;
    }
    
    if (empty($user_id)) return false;   
    
    $select = $this->select()
                    ->where('user_id = ?',$user_id);
                    
     $row = $this->fetchRow($select);
     if (!$row) return false;
     return true;
  }
  public function isManager($country_id, $user_id){
	if(!$user_id) return false;
     $select = $this->select()
                    ->where('country_id = ?',$country_id)
                    ->where('user_id = ?',$user_id);
                    
     $row = $this->fetchRow($select);
     if (!$row){
        $user = Engine_Api::_()->getItem('user',$user_id);
        if ($user && $user->level_id == 1) return true;
        return false;
     } 
     return true;
     
  }
  
  
  public function isAdmin($country_id, $user_id) {
     return $this->isManager($country_id, $user_id);
  }
  
  
  public function removeManager($country_id, $user_id){
     
     $this->delete(array ( 
            'country_id = ? ' => $country_id,
            'user_id = ? ' => $user_id
        )
     );
     
  }
  
  
  
  
         
}
